/*    

    bobbietalk - a visual programming environment for toy robotics

    Copyright (C) 2013  Roberto Tiella

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package bobbietalk.semantics.compiler;

import bobbietalk.gui.ProgrammingBlock;
import bobbietalk.gui.RobotConfiguration;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author roberto
 */
public abstract class AbstractCompiler implements Compiler {

    private File tmpDir = null;
    private String programName = "noname";
    private RobotConfiguration robotConfiguration;

    public AbstractCompiler(String tmpDirPath) {

        tmpDir = new File(tmpDirPath);
    }

    public RobotConfiguration getRobotConfiguration() {
        return robotConfiguration;
    }

    @Override
    public void setRobotConfiguration(RobotConfiguration robotConfiguration) {
        this.robotConfiguration = robotConfiguration;
    }

    @Override
    public boolean compile(List<ProgrammingBlock> blocks, List<String> messages, boolean isSubroutine) {


        BlockGraph g = BlockGraph.fromBlocks(blocks);
        if (!g.checkSemantics(messages)) {
            return false;
        }

        StringBuilder code = new StringBuilder();

        if (!generateCode(g, code, messages, isSubroutine)) {
            return false;
        }

        String pname = programName + "." + getExension();

        File f = new File(tmpDir, pname);

        if (!saveToFile(code.toString(), f, messages)) {
            return false;
        }


        if (!compileWithExternalTool(f.getAbsolutePath(), messages)) {
            return false;
        }

        return true;
    }

    protected abstract boolean generateCode(BlockGraph g, StringBuilder sb, List<String> messages, boolean isSubroutine);

    protected abstract boolean compileWithExternalTool(String ppath, List<String> messages);

    private boolean saveToFile(String code, File f, List<String> messages) {

        PrintWriter pw = null;
        try {
            pw = new PrintWriter(f);
            pw.write(code);
            pw.close();

            return true;

        } catch (FileNotFoundException ex) {
            messages.add("can't write to file:" + f + " reason:" + ex);
            pw.close();
            return false;
        }
    }

    @Override
    public boolean compileOnly(List<ProgrammingBlock> blocks, ArrayList<String> messages, StringBuilder code, boolean isSubroutine) {

        BlockGraph g = BlockGraph.fromBlocks(blocks);
        if (!g.checkSemantics(messages)) {
            return false;
        }


        if (!generateCode(g, code, messages, isSubroutine)) {
            return false;
        }

        return true;
    }

    public void setProgramName(String pn) {
        programName = pn;
    }

    public String getProgramName() {
        return programName;
    }

    public File getTmpDir() {
        return tmpDir;
    }

    public abstract String getExension();
}
